{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Delete Characters to Make Fancy String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: makeFancyString"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除字符使字符串变好"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>一个字符串如果没有 <strong>三个连续</strong>&nbsp;相同字符，那么它就是一个 <strong>好字符串</strong>&nbsp;。</p>\n",
    "\n",
    "<p>给你一个字符串&nbsp;<code>s</code>&nbsp;，请你从 <code>s</code>&nbsp;删除&nbsp;<strong>最少</strong>&nbsp;的字符，使它变成一个 <strong>好字符串</strong> 。</p>\n",
    "\n",
    "<p>请你返回删除后的字符串。题目数据保证答案总是 <strong>唯一的 </strong>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"le<strong>e</strong>etcode\"\n",
    "<b>输出：</b>\"leetcode\"\n",
    "<strong>解释：</strong>\n",
    "从第一组 'e' 里面删除一个 'e' ，得到 \"leetcode\" 。\n",
    "没有连续三个相同字符，所以返回 \"leetcode\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"<strong>a</strong>aab<strong>aa</strong>aa\"\n",
    "<b>输出：</b>\"aabaa\"\n",
    "<strong>解释：</strong>\n",
    "从第一组 'a' 里面删除一个 'a' ，得到 \"aabaaaa\" 。\n",
    "从第二组 'a' 里面删除两个 'a' ，得到 \"aabaa\" 。\n",
    "没有连续三个相同字符，所以返回 \"aabaa\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"aab\"\n",
    "<b>输出：</b>\"aab\"\n",
    "<b>解释：</b>没有连续三个相同字符，所以返回 \"aab\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>s</code>&nbsp;只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [delete-characters-to-make-fancy-string](https://leetcode.cn/problems/delete-characters-to-make-fancy-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [delete-characters-to-make-fancy-string](https://leetcode.cn/problems/delete-characters-to-make-fancy-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"leeetcode\"', '\"aaabaaaa\"', '\"aab\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        a=[]\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in a:\n",
    "                a.append(s[i])\n",
    "        for j in range(len(a)):\n",
    "            while a[j]*3 in s:\n",
    "                s=s.replace(a[j]*3,a[j]*2)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        s=list(s)\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i]==s[i+1] and s[i]==s[i+2]:\n",
    "                s[i]=\"\"\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for c, it in itertools.groupby(s):\n",
    "            l = list(it)\n",
    "            if len(l) > 2:\n",
    "                ans += c * 2\n",
    "            else:\n",
    "                ans += c * len(l)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        st = []\n",
    "        for c in s:\n",
    "            if len(st)<2:\n",
    "                st.append(c)\n",
    "            else:\n",
    "                if c==st[-1] and c==st[-2]:\n",
    "                    continue\n",
    "                else:\n",
    "                    st.append(c)\n",
    "        return \"\".join(st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        s = [i for i in s]\n",
    "        \n",
    "        if len(s)==3:\n",
    "            if len(set([i for i in s])) == 1:\n",
    "                s[0] = ''\n",
    "        else:\n",
    "            for i in range(len(s)-2):\n",
    "                if len(set([j for j in s[i:i+3]])) == 1:\n",
    "                    s[i] = ''\n",
    "            \n",
    "        return ''.join(s)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        if n==1 or n==2:\n",
    "            return s\n",
    "        dp = [0] * n\n",
    "        res = ''+s[0]+s[1]\n",
    "        for i in range(2,n):\n",
    "            if s[i]==s[i-1] and s[i-1]==s[i-2]:\n",
    "                continue\n",
    "            else:\n",
    "                res += s[i]\n",
    "        print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        a=[]\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in a:\n",
    "                a.append(s[i])\n",
    "        for j in range(len(a)):\n",
    "            while a[j]*3 in s:\n",
    "                s=s.replace(a[j]*3,a[j]*2)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        s1=list(s)\n",
    "        #print(s1)\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i]==s[i+1]==s[i+2]:\n",
    "               s1[i+2]=\"\"\n",
    "        #cc=[j for j in s1 if j!=0]\n",
    "        return \"\".join(s1)\n",
    "\n",
    "\n",
    "print(Solution().makeFancyString(s = \"aab\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        if len(s) < 3:\n",
    "            return s\n",
    "        ans = s[0:2]\n",
    "        char1 = 0\n",
    "        char2 = s[0]\n",
    "        for i in range(2,len(s)):\n",
    "            char1, char2 = char2, s[i-1]\n",
    "            if not (s[i] == char1 == char2):\n",
    "                ans += s[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "         n=len(s)\n",
    "         nu=\"\"\n",
    "         d=0\n",
    "         if n<=2:\n",
    "             return s\n",
    "         for i in range(2,n):\n",
    "             if s[i]==s[i-1]==s[i-2]:\n",
    "                 d==1\n",
    "             else:\n",
    "                 nu+=s[i]    \n",
    "         return s[0]+s[1]+nu\n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        if len(s) < 3: return s\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s) - 2:\n",
    "            if s[i] == s[i + 1] == s[i + 2]:\n",
    "                i += 1\n",
    "                continue\n",
    "            ans += s[i]\n",
    "            i += 1\n",
    "        return ans + s[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        if len(s) < 3: return s\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s) - 2:\n",
    "            if not s[i] == s[i + 1] == s[i + 2]:\n",
    "                ans += s[i]\n",
    "            i += 1\n",
    "        return ans + s[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        \n",
    "        if len(s)<=2:\n",
    "            return s\n",
    "        ans = s[0:2]\n",
    "        i=2\n",
    "        n=len(s)\n",
    "        while i<n:\n",
    "            if s[i]==ans[-1] and s[i]==ans[-2]:\n",
    "                i+=1\n",
    "                continue\n",
    "            else:\n",
    "                ans+=s[i]\n",
    "                i+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        if len(s)<=2:\n",
    "            return s\n",
    "        res = s[0]+s[1]\n",
    "        for ch in s[2:]:\n",
    "            if res[-1] == res[-2] == ch:\n",
    "                continue\n",
    "            res+=ch\n",
    "        return res\n",
    "#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        cmp = ''\n",
    "        for i in s:\n",
    "        #     cmp.append(i)\n",
    "        #     print(i,cmp)\n",
    "            if len(cmp) >= 2 and cmp[-1] == cmp[-2] == i:\n",
    "                continue\n",
    "            cmp += i\n",
    "        return cmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        res = \"\"\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i-1]:\n",
    "                i += 1\n",
    "            if i- start >= 3:\n",
    "                res += s[start:start+2]\n",
    "            else:\n",
    "                res += s[start:i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        i, n = 1, len(s)\n",
    "        res, compare = s[0], s[0] \n",
    "\n",
    "        while i < n:\n",
    "            cnt = 1\n",
    "            if s[i] == compare:\n",
    "                res += s[i]\n",
    "                cnt += 1\n",
    "                if cnt == 2:\n",
    "                    cnt = 0\n",
    "                    while i + 1 < n and s[i + 1] == compare:\n",
    "                        i += 1\n",
    "            else:\n",
    "                res += s[i]\n",
    "                compare = s[i] \n",
    "            i += 1\n",
    "\n",
    "        return res\n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        x = 1\n",
    "        k = ''\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            if x == 2:\n",
    "                if i == k:\n",
    "                    continue\n",
    "                x = 1\n",
    "            if i == k:\n",
    "                x += 1\n",
    "            k = i\n",
    "            ans += i \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        # n=len(s)\n",
    "        # ans=''\n",
    "        # left,right=0,0\n",
    "        # while left <n:\n",
    "            \n",
    "        #     while right< n and s[left]==s[right] :\n",
    "        #         if (right-left+1)<3:\n",
    "        #             ans+=s[right]\n",
    "        #             right+=1\n",
    "        #         else:\n",
    "        #             right+=1\n",
    "        #     left=right\n",
    "            \n",
    "        # return ans  \n",
    "        ans=''\n",
    "        for x in s:\n",
    "            if len(ans)>=2 and ans[-1]==ans[-2]==x:\n",
    "                continue\n",
    "            ans+=x\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        flag=''\n",
    "        cflag=0\n",
    "        ans=''\n",
    "        for i in s:\n",
    "            if i != flag:\n",
    "                flag=i\n",
    "                cflag=1\n",
    "            else:\n",
    "                cflag=cflag+1\n",
    "            if cflag <3:\n",
    "                ans=ans+i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        res = \"\"\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i-1]:\n",
    "                i += 1\n",
    "            if i- start >= 3:\n",
    "                res += s[start:start+2]\n",
    "            else:\n",
    "                res += s[start:i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        return \"\".join(char for index, char in enumerate(s) if index < 2 or not (s[index] == s[index - 1] == s[index - 2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        i, n = 1, len(s)\n",
    "        res, compare = s[0], s[0] \n",
    "\n",
    "        while i < n:\n",
    "            cnt = 1\n",
    "            if s[i] == compare:\n",
    "                res += s[i]\n",
    "                cnt += 1\n",
    "                if cnt == 2:\n",
    "                    cnt = 0\n",
    "                    while i + 1 < n and s[i + 1] == compare:\n",
    "                        i += 1\n",
    "            else:\n",
    "                res += s[i]\n",
    "                compare = s[i] \n",
    "            i += 1\n",
    "\n",
    "        return res\n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        i, n = 1, len(s)\n",
    "        res, compare = s[0], s[0] \n",
    "\n",
    "        while i < n:\n",
    "            cnt = 1\n",
    "            res += s[i]\n",
    "            if s[i] == compare:  \n",
    "                cnt += 1\n",
    "                if cnt == 2:\n",
    "                    cnt = 0\n",
    "                    while i + 1 < n and s[i + 1] == compare:\n",
    "                        i += 1\n",
    "            else:\n",
    "                compare = s[i] \n",
    "            i += 1\n",
    "\n",
    "        return res\n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        res = []   # 删除后的字符串\n",
    "        # 遍历 s 模拟删除过程\n",
    "        for ch in s:\n",
    "            if len(res) >= 2 and res[-1] == res[-2] == ch:\n",
    "                # 如果 res 最后两个字符与当前字符均相等，则不添加\n",
    "                continue\n",
    "            # 反之则添加\n",
    "            res.append(ch)\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        res = []\n",
    "        for ch in s:\n",
    "            if len(res) >= 2 and res[-1] == res[-2] == ch:\n",
    "                continue\n",
    "            res.append(ch)\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        res = []   # 删除后的字符串\n",
    "        # 遍历 s 模拟删除过程\n",
    "        for ch in s:\n",
    "            if len(res) >= 2 and res[-1] == res[-2] == ch:\n",
    "                # 如果 res 最后两个字符与当前字符均相等，则不添加\n",
    "                continue\n",
    "            # 反之则添加\n",
    "            res.append(ch)\n",
    "        return \"\".join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        return \"\".join(char  for idx, char in enumerate(s)  if idx < 2 or not (s[idx] == s[idx-1] == s[idx-2]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        res = []   # 删除后的字符串\n",
    "        # 遍历 s 模拟删除过程\n",
    "        for ch in s:\n",
    "            if len(res) >= 2 and res[-1] == res[-2] == ch:\n",
    "                # 如果 res 最后两个字符与当前字符均相等，则不添加\n",
    "                continue\n",
    "            # 反之则添加\n",
    "            res.append(ch)\n",
    "        return \"\".join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        # for循环\n",
    "        pre = '.'\n",
    "        res = []\n",
    "        count = 0  # 已经添加次数\n",
    "        for char in s:\n",
    "            if char == pre:\n",
    "                if count < 2:\n",
    "                    res.append(char)\n",
    "                    count += 1\n",
    "            else:\n",
    "                res.append(char)\n",
    "                count = 1\n",
    "                pre = char\n",
    "        return ''.join(res)\n",
    "\n",
    "        # while循环\n",
    "        res = [s[0]]\n",
    "        i = tmp = 1\n",
    "        while i < len(s):\n",
    "            tmp = tmp + 1 if s[i] == s[i-1] else 1\n",
    "            if tmp < 3: res.append(s[i])\n",
    "            i += 1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        res = []   # 删除后的字符串\n",
    "        # 遍历 s 模拟删除过程\n",
    "        for ch in s:\n",
    "            if len(res) >= 2 and res[-1] == res[-2] == ch:\n",
    "                # 如果 res 最后两个字符与当前字符均相等，则不添加\n",
    "                continue\n",
    "            # 反之则添加\n",
    "            res.append(ch)\n",
    "        return \"\".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        res = []   # 删除后的字符串\n",
    "        # 遍历 s 模拟删除过程\n",
    "        for ch in s:\n",
    "            if len(res) >= 2 and res[-1] == res[-2] == ch:\n",
    "                # 如果 res 最后两个字符与当前字符均相等，则不添加\n",
    "                continue\n",
    "            # 反之则添加\n",
    "            res.append(ch)\n",
    "        return \"\".join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def makeFancyString(self, s: str) -> str:\r\n",
    "        ans = list()\r\n",
    "        for i in s:\r\n",
    "            if not ans:\r\n",
    "                ans.append(i)\r\n",
    "            else:\r\n",
    "                if i != ans[-1]:\r\n",
    "                    ans.append(i)\r\n",
    "                elif len(ans) < 2 or ans[-1] != ans[-2]:\r\n",
    "                    ans.append(i)\r\n",
    "        return ''.join(ans)\r\n",
    "                    \r\n",
    "                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        def aaa(s):\n",
    "            s+='0'\n",
    "            i=1\n",
    "            a=s[0]\n",
    "            for j in range(1,len(s)):\n",
    "                if s[j]==a:\n",
    "                    i+=1\n",
    "                else:\n",
    "                    a=s[j]\n",
    "                    yield i\n",
    "                    i=1\n",
    "        def aab(s):\n",
    "            s+='0'\n",
    "            i=1\n",
    "            a=s[0]\n",
    "            for j in range(1,len(s)):\n",
    "                if s[j]==a:\n",
    "                    i+=1\n",
    "                else:\n",
    "                    a=s[j]\n",
    "                    yield a\n",
    "                    i=1\n",
    "        b1=list(aaa(s))\n",
    "        c1=[s[0]]+list(aab(s))[:-1]\n",
    "        ss=''\n",
    "        for i in range(len(b1)):\n",
    "            if b1[i]<=2:\n",
    "                ss+=b1[i]*c1[i]\n",
    "            else:\n",
    "                ss+=2*c1[i]\n",
    "        return ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        for i in range(len(s)-2):\n",
    "            if len(set(list(s[i:i+3]))) == 1:\n",
    "                s[i] = ''\n",
    "                \n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        return ''.join(i * min(2, sum(1 for _ in g)) for i, g in groupby(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        # 滑动窗口\n",
    "        n = len(s)\n",
    "        p = 0\n",
    "        ans = []\n",
    "        while p < n:\n",
    "            cur = p\n",
    "            while p < n and s[p] == s[cur]: p += 1\n",
    "            ans.append(s[cur]*min(2, p - cur))\n",
    "        \n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        m = []\n",
    "        if len(s)<=2:\n",
    "            return s\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i] == s[i+1] and s[i] == s[i+2]:\n",
    "                continue\n",
    "            else:\n",
    "                m.append(s[i])\n",
    "        m.append(s[-2])\n",
    "        m.append(s[-1])\n",
    "        return ''.join(i for i in m)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        i=2\n",
    "        str_s=s[0:2]\n",
    "        s=list(s)\n",
    "        while i<len(s):\n",
    "            if s[i]==s[i-1] and s[i]==s[i-2]:\n",
    "                pass\n",
    "            else:\n",
    "                str_s+=s[i]       \n",
    "            i+=1\n",
    "       \n",
    "        return str_s\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        ans = []\n",
    "        prev, cnt, begin = None, 0, 0\n",
    "        i, n = 0, len(s)\n",
    "\n",
    "        while i < n:\n",
    "            if s[i] != prev:\n",
    "                prev = s[i]\n",
    "                cnt = 1\n",
    "                i += 1\n",
    "            else:\n",
    "                if cnt == 2:\n",
    "                    ans.append(s[begin:i])\n",
    "                    j = i + 1\n",
    "                    while j < n and s[j] == s[i]:\n",
    "                        j += 1\n",
    "                        \n",
    "                    begin = i = j\n",
    "                    cnt = 1\n",
    "                else:\n",
    "                    cnt += 1\n",
    "                    i += 1\n",
    "\n",
    "        ans.append(s[begin:])\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        return ''.join(next(g) + next(g, '') for _, g in groupby(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        i, n = 1, len(s)\n",
    "        res, ans = s[0], [s[0]]\n",
    "\n",
    "        while i < n:\n",
    "            cnt = 1\n",
    "            if s[i] == ans[-1]:\n",
    "                res += s[i]\n",
    "                cnt += 1\n",
    "                if cnt == 2:\n",
    "                    cnt = 0\n",
    "                    while i + 1 < n and s[i + 1] == ans[-1]:\n",
    "                        i += 1\n",
    "            else:\n",
    "                res += s[i]\n",
    "                ans.append(s[i]) \n",
    "            i += 1\n",
    "\n",
    "\n",
    "\n",
    "        return res\n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        s_1=list(s)\n",
    "        cnt=0\n",
    "        s0=''\n",
    "        s_2=[]\n",
    "        for s1 in s_1:            \n",
    "            if s1==s0:\n",
    "                cnt=cnt+1\n",
    "                if cnt>=2:\n",
    "                    print(1)\n",
    "                else:\n",
    "                    s_2.append(s1)\n",
    "            else:\n",
    "                cnt=0\n",
    "                s_2.append(s1)\n",
    "            s0=s1\n",
    "        s3=''\n",
    "        for s2 in s_2:\n",
    "            s3=s3+str(s2)\n",
    "        return s3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        i, n = 1, len(s)\n",
    "        res, ans = s[0], [s[0]]\n",
    "\n",
    "        while i < n:\n",
    "            cnt = 1\n",
    "            if s[i] == ans[-1]:\n",
    "                res += s[i]\n",
    "                cnt += 1\n",
    "                if cnt == 2:\n",
    "                    cnt = 0\n",
    "                    while i + 1 < n and s[i + 1] == ans[-1]:\n",
    "                        i += 1\n",
    "            else:\n",
    "                res += s[i]\n",
    "                ans.append(s[i]) \n",
    "            i += 1\n",
    "\n",
    "\n",
    "\n",
    "        return res\n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self,s):\n",
    "        lsts = list(s)\n",
    "        tmp = []\n",
    "        result = [1 for i in range(len(lsts))]\n",
    "        final = \"\"\n",
    "        for i in range(len(lsts)):\n",
    "            if lsts[i] in tmp and len(tmp)==2:\n",
    "                result[i] = 0\n",
    "            elif lsts[i] not in tmp:\n",
    "                tmp = []\n",
    "                tmp.append(lsts[i])\n",
    "            else:\n",
    "                tmp.append(lsts[i])\n",
    "        for j in range(len(result)):\n",
    "            if result[j]==1:\n",
    "                final+=lsts[j]\n",
    "        return final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        # 输入：s = \"aaabaaaa\"\n",
    "        # 输出：\"aabaa\"\n",
    "        # 解释：\n",
    "        # 从第一组 'a' 里面删除一个 'a' ，得到 \"aabaaaa\" 。\n",
    "        # 从第二组 'a' 里面删除两个 'a' ，得到 \"aabaa\" 。\n",
    "        # 没有连续三个相同字符，所以返回 \"aabaa\" 。\n",
    "        if len(s) <= 2: return s\n",
    "        l, r = 0, 1\n",
    "        ans = []\n",
    "        while l < len(s):\n",
    "            while r < len(s) and s[r] == s[l]:\n",
    "                r += 1\n",
    "                continue\n",
    "            if r - l >= 3:\n",
    "                ans.append(s[l] * 2)\n",
    "            else:\n",
    "                ans.append(s[l] * (r - l))\n",
    "            l = r\n",
    "            r = l + 1\n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        ans = ['.']\n",
    "        count = 0\n",
    "        for c in s:\n",
    "            if c != ans[-1]:\n",
    "                count = 0\n",
    "                ans.append(c)\n",
    "            elif count < 1:\n",
    "                count += 1\n",
    "                ans.append(c)\n",
    "                \n",
    "        return ''.join(ans[1:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        # 连续相同字符最多两个\n",
    "        # 每次都取出相同的连续的一段，如果>=2个自取2个，否则去1个，拼接所有字符成答案\n",
    "        i,n=0,len(s)\n",
    "        res=[]\n",
    "        while i<n:\n",
    "            start=i\n",
    "            while i<n-1 and s[i]==s[i+1]:\n",
    "                i+=1\n",
    "            if i-start+1>=2:\n",
    "                res.append(s[i]*2)\n",
    "            else:\n",
    "                res.append(s[i])\n",
    "            i+=1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        ans = ['.']\n",
    "        count = 0\n",
    "        for c in s:\n",
    "            if c != ans[-1]:\n",
    "                count = 0\n",
    "                ans.append(c)\n",
    "            elif count < 1:\n",
    "                count += 1\n",
    "                ans.append(c)\n",
    "                \n",
    "        return ''.join(ans[1:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        # slist = [i for i in s]\n",
    "        cnt = 1\n",
    "        k = 1\n",
    "        res = []\n",
    "        while k < len(s):\n",
    "            if s[k] == s[k-1]:\n",
    "                cnt += 1\n",
    "                k+= 1\n",
    "            else:\n",
    "                if cnt > 2:\n",
    "                    res.append(s[k-1]*2)\n",
    "                else:\n",
    "                    res.append(s[k-1]*cnt)\n",
    "                cnt = 1\n",
    "                k+=1\n",
    "        if cnt > 2:\n",
    "            res.append(s[k-1]*2)\n",
    "        else:\n",
    "            res.append(s[k-1]*cnt)\n",
    "        return ''.join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from itertools import groupby\r\n",
    "class Solution:\r\n",
    "    def makeFancyString(self, s: str) -> str:\r\n",
    "        return \"\".join(\"\".join(list(it)[:2]) for _,it in groupby(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        return ''.join(next(g) + next(g, '') for _, g in groupby(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        length = len(s)\n",
    "        ans = []\n",
    "        for ch in s:\n",
    "            if len(ans) >= 2 and ans[-2]== ans[-1] == ch:\n",
    "                continue\n",
    "            ans.append(ch)\n",
    "        return ''.join(ele for ele in ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        if len(s) <= 2:\n",
    "            return s\n",
    "        arr = []\n",
    "        for c in s:\n",
    "            if len(arr) < 2:\n",
    "                arr.append(c)\n",
    "            else:\n",
    "                if arr[-1] == arr[-2] and arr[-1] == c:\n",
    "                    continue\n",
    "                else:\n",
    "                    arr.append(c)\n",
    "        res = ''\n",
    "        for a in arr:\n",
    "            res += a\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        left = 0\n",
    "        s += ' '\n",
    "        ans = []\n",
    "        for right in range(len(s)):\n",
    "            if s[right] == s[left]: continue\n",
    "            if right-left>1: ans.append(s[left]*2)\n",
    "            else: ans.append(s[left])\n",
    "            left = right\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        l=0\n",
    "        r=1\n",
    "        s=s+'1'\n",
    "        cnt=0\n",
    "        res=[]\n",
    "        while l<=r and r<len(s):\n",
    "            if s[l]==s[r]:\n",
    "                cnt+=1\n",
    "                r=r+1\n",
    "            else:\n",
    "                if cnt>=2:\n",
    "                    res.append(s[l:r][0:2])\n",
    "                else:\n",
    "                    res.append(s[l:r])\n",
    "                l=r\n",
    "                r=r+1\n",
    "                cnt=0\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        for i in range(1, len(s)-1):\n",
    "            if s[i] == s[i-1] and s[i-1] == s[i+1]:\n",
    "                s[i-1] = None\n",
    "        return ''.join([i for i in s if i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        list_str = list(s)\n",
    "        result=[]\n",
    "        if len(list_str)>=2:\n",
    "            for j in range(len(list_str)-2):\n",
    "                if list_str[j]==list_str[j+1]==list_str[j+2]:\n",
    "                    pass\n",
    "                else:\n",
    "                    result.append(list_str[j])\n",
    "        \n",
    "            result.append(list_str[-2])\n",
    "            result.append(list_str[-1])\n",
    "        else:\n",
    "            result = list_str\n",
    "\n",
    "        return ''.join(result)\n",
    "        \n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        def returnString(matched):\n",
    "            return matched.group()[:2]\n",
    "        pattern = \"\".join([ch + \"{3,}|\" for ch in string.ascii_lowercase])\n",
    "        return re.sub(pattern, returnString, s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        f, c = 0, 0\n",
    "        a = [-1]\n",
    "        for i in range(n):\n",
    "            if s[i] == f:\n",
    "                c += 1\n",
    "                if c>=3:\n",
    "                    a.append(i)\n",
    "            else:\n",
    "                f = s[i]\n",
    "                c = 1\n",
    "        \n",
    "        if a==[-1]:\n",
    "            return s\n",
    "        ans = ''\n",
    "        for i in range(len(a)-1):\n",
    "            ans += s[a[i]+1:a[i+1]]\n",
    "        return ans + s[a[-1]+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s):\n",
    "        ls=[1]\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                ls.append(ls[i-1]+1)\n",
    "            else:\n",
    "                ls.append(1)\n",
    "        r=\"\"\n",
    "        for i in range(len(ls)):\n",
    "            if ls[i]<3:\n",
    "                r+=s[i]\n",
    "        return r\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        return ''.join(next(g) + next(g, '') for c, g in groupby(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeFancyString(self, s: str) -> str:\n",
    "        i,n=0,len(s)\n",
    "        ans=[]\n",
    "        while i<n:\n",
    "            start=i\n",
    "            while i<n-1 and s[i+1]==s[i]:\n",
    "                i+=1\n",
    "            if i-start+1>=3:\n",
    "                ans.append(s[start:start+2])\n",
    "            else:\n",
    "                ans.append(s[start:i+1])\n",
    "            i+=1\n",
    "        return ''.join(ans)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
